<template>
    <div class="jul-input" :class="{ 'jul-input--suffix': showSuffix }">
        <!-- 如果传了show-password，判断是否需要切换密码的显示，如果不传，不判断 -->
        <input class="jul-input__inner"
        :class="{ 'is-disable': disable }"
        :placeholder="placeholder"
        :type="showPassword ? (passwordVisible ? 'text' : 'password') : type"
        :name="name"
        :disabled="disable"
        :value="value"
        @input="handleInput">
        <span class="jul-input__suffix">
            <i class="jul-input__icon jul-icon-clear" v-if="clearable && value" @click="clear"></i>
            <i class="jul-input__icon" :class="passwordVisible ? 'jul-icon-view' : 'jul-icon-eye-close'" v-if="showPassword && value" @click="handlePassword"></i>
        </span>
    </div>
</template>

<script>
export default {
    name: 'JulInput',
    data() {
        return {
            // 用于控制是否显示密码框
            passwordVisible: false
        }
    },
    props: {
        placeholder: {
            type: String,
            default: ''
        },
        type: {
            type: String,
            default: 'text'
        },
        name: {
            type: String,
            default: ''
        },
        disable: {
            type: Boolean,
            default: false
        },
        value: {
            type: String,
            default: ''
        },
        clearable: {
            type: Boolean,
            default: false
        },
        showPassword: {
            type: Boolean,
            default: false
        }
    },
    computed: {
        showSuffix() {
            return this.clearable || this.showPassword
        }
    },
    methods: {
        handleInput(e) {
            this.$emit('input', e.target.value)
        },
        clear() {
            // 把内容清空
            this.$emit('input', '')
        },
        handlePassword() {
            this.passwordVisible = !this.passwordVisible
        }
    }
}
</script>

<style lang="scss">
    .jul-input {
        width: 100%;
        position: relative;
        font-style: 14px;
        display: inline-block;
        .jul-input__inner {
            -webkit-appearance: none;
            background-color: #fff;
            background-image: none;
            border-radius: 4px;
            border: 1px solid #dcdfe6;
            box-sizing: border-box;
            color: #606266;
            display: inline-block;
            font-style: inherit;
            height: 40px;
            outline: none;
            padding: 0 14px;
            transition: border-color .2s cubic-bezier(.645,.045,.355,1);
            width: 100%;
            &:focus {
                outline: none;
                border-color: #409eff;
            }
            &.is-disable {
                background-color: #acb2be;
                color: #eaecf1;
                border-color: #dcdfe6;
                cursor: not-allowed;
            }
        }
    }

    // 控制小图标的样式 谁用谁加
    .jul-input--suffix {
        .jul-input__inner {
            padding-right: 30px;
        }
        .jul-input__suffix {
            position: absolute;
            height: 100%;
            right: 10px;
            top: 0;
            line-height: 40px;
            text-align: center;
            // color: #c0c4cc;
            transition: all .3s;
            z-index: 999;
            i {
                // color: #c0c4cc;
                // font-style: 14px;
                cursor: pointer;
            }
        }
    }
</style>
